Ext.onReady(function() {

    var store = new Ext.data.Store({
        storeId: 'store1',
        proxy: new Ext.data.HttpProxy({
            url: 'metadata.action?code=property'
        }),
        reader: new Ext.data.JsonReader({
            fields: [{
                    name: 'name'
                }, {
                    name: 'value'
                }]
        })
    });

    store.load();
    var form = new Ext.form.FormPanel({
        baseCls: 'x-plain',
        labelWidth: 70,
        labelHeight: 150,
        fileUpload: true,
        defaultType: 'textfield',
        items: [{
                xtype: 'textfield',
                fieldLabel: '选择文件',
                name: 'userfile',
                id: 'userfile',
                inputType: 'file',
                anchor: '95%' // anchor width by percentage
            }]

    });
    var win_upload = new Ext.Window({
        title: '上传文件',
        width: 450,
        height: 180,
        modal: true,
        x: 100,
        y: 50,
        layout: 'form',
        autoScroll: true,
        constrain: true,
        bodyStyle: 'padding:10px 10px 10px 10px;',
        items: form,
        buttons: [{
                text: '确认上传',
                handler: function() {
                    if (form.form.isValid()) {
                        if (Ext.getCmp('userfile').getValue() == '') {
                            Ext.Msg.alert('提示', '请选择要上传的文件');
                            return;
                        }
                        Ext.MessageBox.show({
                            title: '请稍后....',
                            msg: '文件正在上传中....',
                            progressText: '',
                            width: 300,
                            progress: true,
                            closable: false,
                            animEl: 'loding'
                        });
                        form.getForm().submit({
                            url: basePath + 'servlet/Upload',
                            method: 'POST',
                            success: function(form, action) {
                                Ext.Msg.alert('成功', '文件上传成功！');
                                win_upload.hide();
                                var images = "<br><div><img width='120' height='140'  src='" + basePath + action.result.url + "'/></div>";
                                Ext.getCmp("image_panel").update(images);
                                Ext.getCmp("head_image").setValue(action.result.url);
                            },
                            failure: function(form, action) {
                                Ext.Msg.alert('错误', "文件上传失败，请重新操作！");
                            }
                        })
                    }
                }
            }, {
                text: '关闭',
                handler: function() {
                    win_upload.hide();
                }
            }],
        closable: false,
        draggable: false,
        resizable: false
    });

    var driver = new Ext.FormPanel({
        id: 'driver',
        name: 'driver',
        labelWidth: 80,
        width: '100%',
        height: '100%',
        bodyStyle: 'padding: 4px',
        border: false,
        autoscroll: true,
        frame: true,
        autoScroll: true,
        items: [{
                xtype: "textfield",
                fieldLabel: "驾驶员姓名*",
                emptyText: '驾驶员姓名(必填)',
                disabled: false,
                allowBlank: false,
                id: 'driver_name',
                name: 'name',
                anchor: "80%"
            }, {
                xtype: 'hidden',
                name: 'head_image',
                id: 'head_image'
            }, {
                xtype: 'panel',
                id: 'image_panel',
                fieldLabel: '头像',
                items: [new Ext.Button({
                        text: '选择图片',
                        width: 50,
                        tooltipType: 'title',
                        handler: function() {
                            win_upload.show();

                        }
                    })],
                html: ''
            }, {
                xtype: "radiogroup",
                fieldLabel: "性别",
                emptyText: '性别',
                disabled: false,
                id: 'driver_sex',
                anchor: "80%",
                items: [{
                        boxLabel: '男',
                        name: 'sex',
                        inputValue: '1',
                        checked: true
                    }, {
                        boxLabel: '女',
                        name: 'sex',
                        inputValue: '2'
                    }]
            }, {
                xtype: 'datefield',
                fieldLabel: '出生年月',
                name: 'born',
                id: 'born_date',
                altFormats: 'Ymd',
                format: 'Ymd',
                dateFormat: 'Ymd',
                // columnWidth: .9,
                emptyText: '出生年月',
                anchor: "80%"
            }, {
                xtype: "textfield",
                fieldLabel: "身份证号码",
                emptyText: '身份证号码',
                disabled: false,
                id: 'id_code',
                name: 'id_code',
                anchor: "80%"
            }, {
                xtype: "textfield",
                fieldLabel: "手机号码",
                emptyText: '手机号码',
                disabled: false,
                id: 'phone_num',
                name: 'phone_num',
                anchor: "80%"
            }, {
                xtype: "textfield",
                fieldLabel: "准驾车型",
                emptyText: '准驾车型',
                disabled: false,
                id: 'car_model',
                name: 'car_model',
                anchor: "80%"
            }, {
                xtype: "radiogroup",
                fieldLabel: "是否兼职",
                emptyText: '',
                disabled: false,
                id: 'driver_jianzhi',
                anchor: "80%",
                items: [{
                        boxLabel: '是',
                        name: 'sideline',
                        inputValue: 'yes'
                    }, {
                        boxLabel: '否',
                        name: 'sideline',
                        inputValue: 'no',
                        checked: true
                    }]
            }, {
                xtype: "textfield",
                fieldLabel: "工作经历",
                emptyText: '工作经历',
                disabled: false,
                id: 'experience',
                name: 'experience',
                anchor: "80%"
            }, {
                xtype: "textfield",
                fieldLabel: "个人荣誉",
                emptyText: '个人荣誉',
                disabled: false,
                id: 'glory',
                name: 'glory',
                anchor: "80%"
            },
            {
                xtype: "textfield",
                fieldLabel: "缩写",
                emptyText: '缩写',
                disabled: false,
                id: 'ab',
                name: 'ab',
                anchor: "80%"
            }]
    });

    var driver_win = new Ext.Window({
        id: 'driver_win',
        title: "添加驾驶员",
        border: false,
        bodyBorder: false,
        // layout:'fit',
        width: 415,
        closeAction: 'hide',
        modal: true,
        tbar: ['-', {
                text: ' 提交并继续',
                handler: function() {
                    onSubmit();
                }
            }, '-', {
                text: '重置',
                handler: function() {
                    driver.getForm().reset();
                }
            }],
        items: [driver]
    });

    function onSubmit() {
        // 提交表单
        if (driver.getForm().isValid()) {
            var submitValues = driver.getForm().getValues();
            for (var param in submitValues) {
                if (driver.form.findField(param)
                        && driver.form.findField(param).emptyText == submitValues[param]) {
                    driver.form.findField(param).setValue(' ');
                }
            }
            driver.getForm().submit({
                url: 'DriverAction.action',
                waitMsg: '数据保存中...',
                submitEmptyText: false,
                success: function(c, d) {
                    Ext.MessageBox.alert("操作信息", "成功信息保存！");
                    driver.getForm().reset();
                },
                failure: function(c, d) {
                    Ext.MessageBox.show({
                        title: "操作信息",
                        msg: "信息保存出错，请联系管理员！",
                        buttons: Ext.MessageBox.OK,
                        icon: "ext-mb-error"
                    });
                }
            });
        }
    }

    driver_win.show();
})